<?php

namespace app\models\search;

use app\models\CrmHwzx;
use app\models\CrmHwzxZccl;
use yii\base\Model;
use yii\data\ActiveDataProvider;

class CrmHwzxZcclSearch extends \app\models\CrmHwzxZccl
{
    public function rules()
    {
        return [
            [['id','gsid'], 'integer'],
            [['ldhm','bh','zzjxsj','ldzcsj','dhfsj','khmc','khxb','dhhm','gtnr','gtnr','zxcp','khbz','gsy','khzl'], 'safe'],
        ];
    }

    public function scenarios()
    {
        return Model::scenarios();
    }

    /**
     * @param array $传递的参数，数组
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = CrmHwzxZccl::find();
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        //设置前端传来的需要查询的数据
        $this->load($params,"CrmHwzxZcclSearch");
        if (!$this->validate()) {
            return $dataProvider;
        }
        $query
            ->andFilterWhere(['like', 'khmc', $this->khmc])
            ->andFilterWhere(['like', 'ldhm', $this->ldhm])
            ->andFilterWhere(['like', 'zxcp', $this->zxcp])
            ->andFilterWhere(['=', 'gsid', $this->gsid])
            ->andFilterWhere(['=', 'khzl', $this->khzl])
        ;//接听电话
        if ($params['CrmHwzxZcclSearch']['sjlx']!=="" && !empty($params['CrmHwzxZcclSearch']['sjqj'])){
            $timeZone = $params['CrmHwzxZcclSearch']['sjqj'];
            switch ($params['CrmHwzxZcclSearch']['sjlx']){
                case "0":
                    $query->andFilterWhere(['between', 'zzjxsj',$timeZone[0] ,$timeZone[1]]);
                    break;
                case "1":
                    $query->andFilterWhere(['between', 'ldzcsj',$timeZone[0] ,$timeZone[1]]);
                    break;
                case "2":
                    $query->andFilterWhere(['between', 'dhfsj',$timeZone[0] ,$timeZone[1]]);
                    break;
            }
        }
        $query->select('c.*,u.username,y.yggh');
        $query->from('crm_hwzx_zccl as c');
        $query->leftJoin('user as u','u.id=c.gsid');
        $query->leftJoin('crm_system_ygxx as y','u.id=y.user_id');
        $query->andFilterWhere(['like', 'y.yggh', $params['CrmHwzxZcclSearch']['yggh']]);
        return $dataProvider;
    }
}
